<?php

namespace App\Controller\App;

use App\Base\BaseController4Restful;
use App\Entity\UserAddress;
use App\Repository\UserAddressRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @route("/app")
 */
class UserAddressController extends BaseController4Restful
{
    /**
     * @Route("/set/my/default/address/{id}", name="AppUserAddress",methods={"GET","POST"})
     */
    public function AppUserAddress(UserAddress $userAddress)
    {
        $user = $this->needToBeUser();
        $list = $user->getAddresses();
        $find = 0;
        foreach ($list as $addr) {
            if ($addr->getId() == $userAddress->getId()) {
                $addr->setIsDefault(1);
                $find =1;
            }else{
                $addr->setIsDefault(0);
            }
            $this->myEm()->persist($addr);
        }
        if ($find) {
            $this->myEm()->flush();
            return $this->json_out(200,"设置成功");
        }else{
            return $this->json_out(500,"数据异常");
        }
    }
}
